package book;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-10
 * Time: 13:07
 */
//class ListNde{
//    private Book book;
//    private ListNde next;
//
//    public ListNde(){
//        Book book1 = new Book("西游记","吴承恩",57.9,"小说");
//        Book book2 = new Book("三国演义","罗贯中",85.9,"小说");
//        Book book3 = new Book("水浒传","施耐庵",77.9,"小说");
//        Book book4 = new Book("红楼梦","曹雪芹",65.9,"小说");
//    }
//}
public class BookList {

    private Book[] books = new Book[10];
    //数组的有效数据个数
    private int userSize; //图书的数量

    public BookList() {
        books[0] = new Book("西游记","吴承恩","小说",57.9);
        books[1] = new Book("三国演义","罗贯中","小说",85.9);
        books[2] = new Book("水浒传","施耐庵","小说",77.9);
        books[3] = new Book("红楼梦","曹雪芹","小说",65.9);
        this.userSize = 3;
    }

    public int getUserSize() {
        return userSize;
    }

    public void setUserSize(int userSize) {
        this.userSize = userSize;
    }

    /**
     * 获取 pos 位置的一本书
     * @param pos
     * @return
     */
    public Book getPos(int pos){
            return this.books[pos];
    }

    //判断数组是否满了
    public boolean ifFull(){
        return this.userSize == this.books.length;
    }

    /**
     * 在 pos 位置添加一本书
     * @param pos
     * @param book
     */
    public void setBook(int pos,Book book){
        if(pos<0 || pos>this.userSize){
            System.out.println("位置不合法");
            return;
        }
        //如果满了就进行扩容
        if(ifFull()){
            books = Arrays.copyOf(books,books.length*2);
        }
        this.books[pos] = book;
    }

}
